package zmee.pmct.formplugin.pmas;


import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.utils.StringUtils;
import kd.bos.entity.datamodel.events.ChangeData;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.control.AttachmentPanel;
import kd.bos.form.control.Control;
import kd.bos.form.control.EntryGrid;
import kd.bos.form.control.events.RowClickEvent;
import kd.bos.form.control.events.RowClickEventListener;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.operate.FormOperate;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.servicehelper.AttachmentServiceHelper;
import org.apache.commons.collections.CollectionUtils;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.pmgt.pmbs.common.model.pmct.ContractTplConstant;
import zmee.util.BosUtil;

import java.util.EventObject;
import java.util.List;
import java.util.Map;

/**
 * @author lijiawang
 * @Date 2022/12/7 14:30
 * @Description:支出合同
 */
public class PmctOutcontractPlugin extends AbstractFormPlugin implements RowClickEventListener {
    private static Log log = LogFactory.getLog(PmctOutcontractPlugin.class);

    @Override
    public void registerListener(EventObject e) {
        // 单据体行点击
        EntryGrid entryGrid = this.getView().getControl("listmodelentry");
        if (entryGrid != null) {
            entryGrid.addRowClickListener((RowClickEventListener) this);
        }
    }

    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs args) {
        FormOperate operate = (FormOperate) args.getSource();
        String key = operate.getOperateKey();
        if ("submit".equals(key)) {
            try {
                AttachmentPanel attachmentpanel = (AttachmentPanel) this.getView().getControl("zmee_contractaccessory");
                List<Map<String, Object>> attachmentData = attachmentpanel.getAttachmentData();
                if (CollectionUtils.isNotEmpty(attachmentData)) {
                    for (Map<String, Object> attach : attachmentData) {
                        String name = attach.get("name").toString();
                        log.info("attachment name：" + name);
                        if (name.contains("打印模板")) {
                            // 删除附件
                            log.info(String.format("开始删除单据标识为:%s ,单据编号为:%s ,ID为:%s ,附件名称为:%s的附件",
                                    attach.get("entityNum").toString(), this.getModel().getValue("billno"), attach.get("attPkId"), name));
                            AttachmentServiceHelper.remove(attach.get("entityNum").toString(), attach.get("attPkId"), attach.get("uid"));
                            attachmentpanel.remove(attach);
                        }
                    }
                }
                BosUtil.createAtt("pmct_outcontract_prtpl", this.getModel(), this.getView(), "zmee_contractaccessory", "pmct_outcontract");

            } catch (Exception e) {
                log.info("附件生成" + e);
            }
        }
    }

    @Override
    public void afterBindData(EventObject e) {
        super.afterBindData(e);
        boolean isonlist = (boolean) this.getModel().getValue(ContractTplConstant.Isonlist);
        if (isonlist) {
            DynamicObjectCollection entryEntity = this.getModel().getEntryEntity(ContractTplConstant.EntryEntityId_listmodelentry);
            if (entryEntity.isEmpty()) {
                return;
            }
            this.getView().setVisible(Boolean.TRUE, ContractTplConstant.EntryEntityId_treelistentry);
            DynamicObjectCollection sublistentry = this.getModel().getEntryEntity("sublistentry");
            DynamicObjectCollection treelistentry = this.getModel().getEntryEntity("treelistentry");
            for (int i = 0; i < sublistentry.size(); i++) {
                if (treelistentry.size() == sublistentry.size()) {
                    this.getModel().setValue("zmee_treerowtype", sublistentry.get(i).getDynamicObject("zmee_rowtype"), i);//行类型
                    this.getModel().setValue("zmee_easnumber0", sublistentry.get(i).getString("zmee_easnumber"), i);//EAS采购申请单号
                    this.getModel().setValue("zmee_easentry0", sublistentry.get(i).getString("zmee_easentry"), i);//EAS行号
                }
            }
        }
    }

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        String name = e.getProperty().getName();
        ChangeData[] changeSet = e.getChangeSet();
        ChangeData data = changeSet[0];
        //行类型
        if (name.equals("zmee_treerowtype")) {
            int rowIndex = data.getRowIndex();
            DynamicObject sublistentry = this.getModel().getEntryRowEntity("sublistentry", rowIndex);
            sublistentry.set("zmee_rowtype", data.getNewValue());
            this.getView().updateView("sublistentry");
        }

        if (name.equals("treelistingbudgetitem")) {
            Object rowValue = data.getNewValue();
            int rowIndex = data.getRowIndex();
            if (rowValue == null) {
                this.getModel().setValue("listingbudgetitem", rowValue, rowIndex);
            }
        }

        if (name.equals("treematerial")) {
            Object rowValue = data.getNewValue();
            int rowIndex = data.getRowIndex();
            if (rowValue == null) {
                this.getModel().setValue("material", rowValue, rowIndex);
            }
        }

        if (name.equals("zmee_oauserfield")) {
            Object newValue = data.getNewValue();
            if (newValue != null) {
                DynamicObject row = (DynamicObject) newValue;
                DynamicObject user = BusinessDataServiceHelper.loadSingle(row.getPkValue(), row.getDynamicObjectType().getName());
                DynamicObjectCollection entryentity = user.getDynamicObjectCollection("entryentity");
                for (DynamicObject ent : entryentity) {
                    if (!ent.getBoolean("ispartjob")) {
                        this.getModel().setValue("htcbbm", ent.getString("dpt.id"));
                        break;
                    }
                }
            } else {
                this.getModel().setValue("htcbbm", newValue);
            }
        }
    }

    @Override
    public void entryRowClick(RowClickEvent evt) {
        RowClickEventListener.super.entryRowClick(evt);
        Control source = (Control) evt.getSource();
        if (StringUtils.equals("listmodelentry", source.getKey())) {
            int rowIndex = evt.getRow();
            DynamicObjectCollection listModelEntry = this.getModel().getEntryEntity("listmodelentry");
            DynamicObjectCollection sublistentry = listModelEntry.get(rowIndex).getDynamicObjectCollection("sublistentry");
            if (sublistentry == null || sublistentry.size() == 0) {
                return;
            }
            DynamicObjectCollection treelist = this.getModel().getEntryEntity("treelistentry");
            for (int i = 0; i < sublistentry.size(); i++) {
                this.getModel().setValue("zmee_treerowtype", sublistentry.get(i).get("zmee_rowtype"), i);
                this.getModel().setValue("zmee_easnumber0", sublistentry.get(i).get("zmee_easnumber"), i);
//                treelist.get(i).set("zmee_rowtype",sublistentry.get(i).get("zmee_treerowtype"));
            }
        }
    }
}
